package com.wanmait.travel.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class Pager {
	private List data;
	private int pageNum;
	private int rowCount;
	private int pageSize = 10;
	public boolean hasPrev() {
		return pageNum>1;
	}
	public boolean hasNext() {
		return pageNum<this.getPageCount();
	}
	
	public static Pager init(HttpServletRequest request) {
		String pageNumStr = request.getParameter("pageNum");
		String pageSizeStr = request.getParameter("pageSize");
		int  pageNum = 1;
		try {
			pageNum = Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
		}
		
		int pageSize = 10;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (NumberFormatException e) {
		}
		
		Pager pager = new Pager();
		pager.setPageNum(pageNum);
		pager.setPageSize(pageSize);
		request.setAttribute("pager", pager);
		return pager;
	}
	public int getPageCount() {
		if(rowCount<0) return 1;
		return rowCount/pageSize+(rowCount%pageSize==0?0:1);
	}
	public List getData() {
		return data;
	}
	public void setData(List data) {
		this.data = data;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
}
